//
//  TRLeftViewController.m
//  WeiMiLan
//
//  Created by Mac on 14-8-12.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRLeftViewController.h"
#import "TRTabBarViewController.h"
#import "TREmptyViewController.h"

@interface TRLeftViewController ()
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;
@property (strong, nonatomic) TRGaoShouController *vc;
@property (strong, nonatomic) TREmptyViewController *emptyVC;
@end

@implementation TRLeftViewController

- (TRGaoShouController *)vc {
    if (_vc == nil) {
        _vc = [[TRGaoShouController alloc]init];
    }
    return _vc;
}

- (UIViewController *)emptyVC {
    if (_emptyVC == nil) {
        _emptyVC = [[TREmptyViewController alloc]init];
    }
    return _emptyVC;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (IBAction)tap:(UITapGestureRecognizer *)sender {
    
    [self.markTextField resignFirstResponder];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    for (UIView* v in self.view.subviews) {
        if ([v isMemberOfClass:[UITextField class]]) {
            
            v.layer.borderColor = [UIColor colorWithRed:203/255. green:155/255. blue:15/255. alpha:1].CGColor;
            v.layer.borderWidth = 1.f;
            v.layer.cornerRadius = 5.f;
            v.layer.masksToBounds = YES;
            ((UITextField *)v).font = [UIFont systemFontOfSize:12.f];
            ((UITextField *)v).textColor = [UIColor whiteColor];
            [v setValue:[UIColor colorWithRed:92/255. green:92/255. blue:92/255. alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
            [v setValue:[UIFont boldSystemFontOfSize:10] forKeyPath:@"_placeholderLabel.font"];
            
            v.backgroundColor = [UIColor colorWithRed:44/255. green:44/255. blue:44/255. alpha:1];
        }
    }
    self.delegate=ApplicationDelegate;
    self.list=@[@"女包",@"男包",@"服装",@"鞋子",@"手表",@"皮带",@"钱夹",@"丝巾饰品",@"眼镜"];
    //当前版本
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    
    
    self.versionLabel.text = [NSString stringWithFormat:@"版本：V%@",appVersion];
    
//    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"menu_left_bg"]];
    self.view.backgroundColor = COLOR(49, 49, 49, 1.0);
    
    if ([QLPropertyUtils getBoolByKey:@"guid_slip" defaultValue:NO]==NO) {
        self.viewGuide.hidden = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(guideTap:)];
        [self.viewGuide addGestureRecognizer:tap];
    }
}

- (void)guideTap:(id)sender
{
    self.viewGuide.hidden = YES;
    [QLPropertyUtils putBoolByKey:@"guid_slip" value:YES];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateUserInfo];
}

- (void)updateUserInfo
{
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    NSString *headerUrl = [userInfo ql_stringForKey:@"HEAD_PIC_URL"];
    
    if ([[userInfo ql_stringForKey:@"HEAD_PIC_URL"] containsString:@"upload/users"]) {
        headerUrl = [[NSUserDefaults standardUserDefaults]objectForKey:@"urlHeader"];
    } else {
        headerUrl = [userInfo ql_stringForKey:@"HEAD_PIC_URL"];
    }
    
    self.imageViewUserHeader.layer.masksToBounds = YES;
    self.imageViewUserHeader.layer.cornerRadius = 31; //设置图片圆角的尺度
    
    if ([[QLUserManager shareInstance] isLoginUser]) {
        NSString *nickname = [userInfo ql_stringForKey:@"NAME"];
        self.labelUserName.text = nickname ==nil?[QLPropertyUtils getStringByKey:@"account" defaultValue:nil]:nickname;
        [self.imageViewUserHeader setImageWithURL:[NSURL URLWithString:headerUrl] placeholderImage:[UIImage imageNamed:@"nodata_avator"]];
    }else{
        self.labelUserName.text = @"尚未登录";
        [self.imageViewUserHeader setImage:[UIImage imageNamed:@"nodata_avator"]];
    }
}

- (IBAction)buttonAllAction:(UIButton *)sender
{
    [self showAll];
    [self.markTextField resignFirstResponder];
}

-(void)showAll{
    self.markTextField.text=@"";
    ApplicationDelegate.markString=@"";
    ApplicationDelegate.markDictionary=nil;
    [SVProgressHUD showSuccessWithStatus:@"显示全部"];
    //[ApplicationDelegate.rootVC.mydelegate menuViewDidSelecte:-1];
    //root刷新
    [QLPropertyUtils putStringByKey:@"BID" value:@""];
    
    //切换到分类tab
    TRTabBarViewController *tabVc =ApplicationDelegate.rootVC;
    [tabVc.customTabBarView changAction:tabVc.customTabBarView.button2];;
    for (UINavigationController *nv in tabVc.viewControllers) {
        UIViewController *vc = [nv.viewControllers firstObject];
      //  NSLog(@"%@",[vc class]);
        if ([vc isMemberOfClass:[TRTypeViewController class]]) {
            TRTypeViewController * menuVc =(TRTypeViewController *)vc;
            if ([menuVc respondsToSelector:@selector(searchData:)]) {
                [menuVc performSelector:@selector(searchData:) withObject:nil afterDelay:0.5];
            }
        }
    }
    ApplicationDelegate.markIsChang=YES;
    [ApplicationDelegate.ddVC showRootController:YES];
}

- (IBAction)buttonSeacherAction:(id)sender
{
    TRTabBarViewController * tabVc =ApplicationDelegate.rootVC;
    IIIViewController *vc = [[QLPlayerManager getStoryboard] instantiateViewControllerWithIdentifier:@"IIIViewController"];
    [(UINavigationController*)tabVc.selectedViewController pushViewController:vc animated:YES];
    [ApplicationDelegate.ddVC showRootController:YES];
//    self.emptyVC.navTitle = @"概念题材";
//    [self presentViewController:self.emptyVC animated:YES completion:nil];
    
    
}


- (IBAction)buttonShareAction:(id)sender
{
    TRTabBarViewController *tabVc =ApplicationDelegate.rootVC;
    [ApplicationDelegate.ddVC showRootController:YES];
    UINavigationController *nav = [tabVc.viewControllers objectAtIndex:3];
    TRMyhomeViewController *vc = (TRMyhomeViewController*)nav.visibleViewController;
    [vc buttonYaoqingAction:nil];
}

- (IBAction)buttonPinpaiAction:(id)sender
{
//    TRTabBarViewController * tabVc =ApplicationDelegate.rootVC;
//    IIIViewController *vc = [[QLPlayerManager getStoryboard] instantiateViewControllerWithIdentifier:@"IIIViewController"];
//    [(UINavigationController*)tabVc.selectedViewController pushViewController:vc animated:YES];
//    [ApplicationDelegate.ddVC showRootController:YES];
   

    [self presentViewController:self.vc animated:YES completion:nil];
}

- (IBAction)buttonMyhomeAction:(TRButton *)sender
{
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    [QLPlayerManager openUserHomeWithId:[userInfo ql_intForKey:@"ID"] viewController:ApplicationDelegate.rootVC                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ];
}

- (IBAction)toMyReputation:(UIButton *)sender
{
////    NSString *str = @"itms-apps://itunes.apple.com/app/id904191053";
////    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
////    [ApplicationDelegate.ddVC showRootController:YES];
//     self.emptyVC.navTitle = @"评价";
//   //  [self.emptyVC addChildViewController:self];
//     [self presentViewController:self.emptyVC animated:YES completion:nil];
    NSString *str = @"https://itunes.apple.com/app/id1054652875";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    [ApplicationDelegate.ddVC showRootController:YES];
}

- (IBAction)buttonUserDetailAction:(id)sender
{
    TRTabBarViewController *tabVc =ApplicationDelegate.rootVC;
    [ApplicationDelegate.ddVC showRootController:YES];
    
    TRUserDetailViewController *vc = [[TRUserDetailViewController alloc] init];
    [(UINavigationController*)tabVc.selectedViewController pushViewController:vc animated:YES];
}

@end
